package com.pb.infra.auth.controller;

import com.pb.infra.auth.entity.custom.LedingOutFile;
import com.pb.infra.auth.service.LeadingOutService;
import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.util.Constant;
import com.pb.infra.oth.util.FileUtil;
import com.pb.infra.oth.util.RedisUtil;
import com.pb.infra.oth.util.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 异步导出excel
 *
 * @Author:liaohaoyuan
 * @Date: 2021/04/26
 */
@Slf4j
@RestController
@RequestMapping("v1/sys/LeadingOut")
public class LeadingOutController {

    /**
     * 业务逻辑对象
     */
    @Resource
    private LeadingOutService leadingOutService;

    /**
     * redis封装类
     */
    @Resource
    private RedisUtil redisUtil;

    /**
     * 日志操作表
     */
    private static final String TABLE_NAME = "FILE_DOWNLOAD";

    /**
     * 日志操作功能名
     */
    private static final String OPERATION_MENU = "临时文件管理";

    /**
     * 获取临时文件信息
     * @param paraMap 请求参数
     * @return LedingOutFile 返回数据
     */
    @GetMapping("getFileJudgeData")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public LedingOutFile getFileJudgeData(@RequestParam Map<String,Object> paraMap){
        String fileKey = (String) paraMap.get("fileKey");
        LedingOutFile leding = (LedingOutFile)redisUtil.get(fileKey);
        return leding;
    }

    /**
     * 查询所有临时文件
     * @param request
     * @return Result 返回数据
     */
    @GetMapping("selectAllFile")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result selectAllFile(@RequestParam Map<String, Object> paramMap, Page page, HttpServletRequest request){
        return leadingOutService.selectAllFile(request, page, paramMap);
    }

    /**
     * 下载文件
     * @param request
     * @param response
     * @param ledingOutFile 请求参数
     * @throws Exception
     */
    @PostMapping("/down")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DOWN, menuName = OPERATION_MENU)
    public void downloadImage(HttpServletRequest request, HttpServletResponse response, @RequestBody LedingOutFile ledingOutFile ) throws Exception{
        String fileUrl = ledingOutFile.getFilePath();
        if(fileUrl != null){
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/octet-stream");
            String fileName = ledingOutFile.getFileName();
            if(StringUtils.isNotEmpty(fileName)) {
                fileName.replaceAll("\\r\\n","");
                fileName.replaceAll("%0d%0a","");
                response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
            }else{
                response.setHeader("Content-Disposition", "attachment;fileName=");
            }
            //将文件转换成二进制文件
            FileUtil.writeBytes(fileUrl,response.getOutputStream());
        }
    }

}
